package com.example.es.service;


import com.alibaba.fastjson.JSON;
import com.example.es.pojo.Content;
import com.example.es.util.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ContentService {


	@Autowired
	private RestHighLevelClient restHighLevelClient;


	//解析京东数据放入es
	public Boolean parseContent(String keyword) throws IOException {
		List<Content> contents= HtmlParseUtil.paresJD(keyword);

		BulkRequest bulkRequest=new BulkRequest();
		bulkRequest.timeout("2m");

		for (Content content : contents) {
			bulkRequest.add(new IndexRequest("jd_goods").source(JSON.toJSONString(content), XContentType.JSON));
		}

		BulkResponse bulkResponse=restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
		return  !bulkResponse.hasFailures();
	}

	public List<Map<String,Object>> search(String keyword,int pageNo,int pageSize) throws IOException {
		if(pageNo<=1){
			pageNo=1;
		}
		//条件搜索
		SearchRequest searchRequest=new SearchRequest("jd_goods");
		SearchSourceBuilder sourceBuilder=new  SearchSourceBuilder();

		//分页
		sourceBuilder.from(pageNo);
		sourceBuilder.size(pageSize);
		//精准匹配
		sourceBuilder.query(QueryBuilders.matchQuery("title",keyword));
		sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

		//代码高亮
		HighlightBuilder highlightBuilder=new HighlightBuilder();
		highlightBuilder.field("title");
		highlightBuilder.preTags("<font style='color: #e4393c;'>");
		highlightBuilder.postTags("</font>");
		sourceBuilder.highlighter(highlightBuilder);


		searchRequest.source(sourceBuilder);

		SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
		List<Map<String,Object>> result=new ArrayList<>();
		for (SearchHit hit : searchResponse.getHits().getHits()) {
			Map<String, HighlightField> highlightFieldMap=hit.getHighlightFields();
			HighlightField title=highlightFieldMap.get("title");

			Map<String,Object> sourceAsMap=hit.getSourceAsMap();

			//替换高亮内容
			if(title!=null){
				Text[] fragments=title.fragments();

				String  newTitle="";
				for (Text text : fragments) {
					newTitle+=text;
				}
				sourceAsMap.put("title",newTitle);
			}
			result.add(sourceAsMap);
		}
		return result;
	}
}
